package com.example.supplychainbackend.controller;

import com.example.supplychainbackend.controller.dto.DataResponse;
import com.example.supplychainbackend.controller.dto.blockchain.UploadInfo;
import com.example.supplychainbackend.controller.dto.design.BOMDto;
import com.example.supplychainbackend.controller.dto.design.ECNDto;
import com.example.supplychainbackend.controller.dto.design.OrderDto;
import com.example.supplychainbackend.controller.dto.user.UserSigDto;
import com.example.supplychainbackend.service.DesignService;
import com.example.supplychainbackend.service.SalesService;
import com.example.supplychainbackend.tool.JwtUtil;
import jakarta.transaction.Transactional;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/design")
@AllArgsConstructor
public class DesignController {
    private final DesignService designService;
    private final SalesService salesService;
    
    @GetMapping("/order/{id}")
    public DataResponse<OrderDto> getOrder(@PathVariable String id) {
        OrderDto res = salesService.getDesignOrder(id);
        return DataResponse.success(res);
    }

    @GetMapping("/order/list")
    public DataResponse<List<OrderDto>> listOrders(@RequestHeader("Authorization") String authHeader) {
        List<OrderDto> res = salesService.getAllDesignOrders();
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        String eid = userSigDto.getEnterpriseId();
        if (eid != null) {
            res = salesService.getAllDesignOrdersByEid(eid);
        }
        return DataResponse.success(res);
    }

//    @PostMapping("/bom/{id}/chain")
//    public DataResponse<UploadInfo> uploadToChainBOM(@PathVariable String id) {
//        UploadInfo txInfo = designService.uploadToChainBOM(id);
//        return DataResponse.success(txInfo);
//    }

    @PostMapping("/bom")
    public DataResponse<String> createBOM(@RequestHeader("Authorization") String authHeader,
                                          @RequestBody BOMDto form) {
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        form.setUserName(userSigDto.getUserName());
        form.setUserId(userSigDto.getUserId());
        form.setEnterpriseName(userSigDto.getEnterpriseName());
        form.setEnterpriseId(userSigDto.getEnterpriseId());
        String materialCode = designService.createBOM(form);
        return DataResponse.success(materialCode);
    }

    @PutMapping("/bom/{id}")
    public DataResponse<BOMDto> updateBOM(@RequestHeader("Authorization") String authHeader,
                                          @PathVariable String id, @RequestBody BOMDto form) {
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        form.setUserName(userSigDto.getUserName());
        form.setUserId(userSigDto.getUserId());
        form.setEnterpriseName(userSigDto.getEnterpriseName());
        form.setEnterpriseId(userSigDto.getEnterpriseId());
        BOMDto res = designService.updateBOM(id, form);
        return DataResponse.success(res);
    }

    @GetMapping("/bom/list")
    public DataResponse<List<BOMDto>> listAllBOMs() {
        List<BOMDto> res = designService.getAllBOMs();
        return DataResponse.success(res);
    }

    @GetMapping("/bom/list/{oid}")
    public DataResponse<List<BOMDto>> listBOMsByOrder(@PathVariable String oid) {
        List<BOMDto> res = designService.getBOMsByOrder(oid);
        return DataResponse.success(res);
    }

    @GetMapping("/bom/{id}")
    public DataResponse<BOMDto> getBOM(@PathVariable String id) {
        BOMDto res = designService.getBOMByCode(id);
        return DataResponse.success(res);
    }

    @DeleteMapping("/bom/{id}")
    @Transactional
    public DataResponse<Long> deleteBOM(@RequestHeader("Authorization") String authHeader,
                                        @PathVariable String id) {
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        designService.deleteBOM(id, userSigDto);
        return DataResponse.success(1L);
    }

    @PostMapping("/ecn/{id}/chain")
    public DataResponse<UploadInfo> uploadToChainECN(@PathVariable String id) {
        UploadInfo txInfo = designService.uploadToChainECN(id);
        return DataResponse.success(txInfo);
    }

    @PostMapping("/ecn")
    public DataResponse<String> createECN(@RequestHeader("Authorization") String authHeader,
                                          @RequestBody ECNDto form) {
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        form.setUserName(userSigDto.getUserName());
        form.setUserId(userSigDto.getUserId());
        form.setEnterpriseName(userSigDto.getEnterpriseName());
        form.setEnterpriseId(userSigDto.getEnterpriseId());
        String ecnNumber = designService.createECN(form);
        return DataResponse.success(ecnNumber);
    }

    @PutMapping("/ecn/{id}")
    public DataResponse<ECNDto> updateECN(@RequestHeader("Authorization") String authHeader,
                                          @PathVariable String id, @RequestBody ECNDto form) {
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        form.setUserName(userSigDto.getUserName());
        form.setUserId(userSigDto.getUserId());
        form.setEnterpriseName(userSigDto.getEnterpriseName());
        form.setEnterpriseId(userSigDto.getEnterpriseId());
        ECNDto res = designService.updateECN(id, form);
        return DataResponse.success(res);
    }

    @GetMapping("/ecn/list")
    public DataResponse<List<ECNDto>> listECNs(@RequestHeader("Authorization") String authHeader) {
        List<ECNDto> res = designService.getAllECNs();
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        String eid = userSigDto.getEnterpriseId();
        if (eid != null) {
            res = designService.getAllECNsByEid(eid);
        }
        return DataResponse.success(res);
    }

    @GetMapping("/ecn/{id}")
    public DataResponse<ECNDto> getECN(@PathVariable String id) {
        ECNDto res = designService.getECN(id);
        return DataResponse.success(res);
    }

    @DeleteMapping("/ecn/{id}")
    @Transactional
    public DataResponse<Long> deleteECN(@PathVariable String id) {
        designService.deleteECN(id);
        return DataResponse.success(1L);
    }

    @ExceptionHandler(Exception.class)
    public DataResponse<Exception> designErrorHandler(Exception e) {
        return DataResponse.error(500,e);
    }

}
